<?php

/**
 * users actions.
 *
 * @package    master_directory
 * @subpackage users
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class usersActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$this->form = new UserForm();
    
    $this->directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $this->users = Doctrine_Core::getTable('User')->getAll();
    
    $this->roles = Doctrine_Core::getTable('UserRol')->getAll();
    
    $this->selected_directories_ids = array();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id')));
    
    $this->forward404Unless($this->user);
    
   	$users = Doctrine_Core::getTable('User')->getAll();
    
    $form = new UserForm($this->user);
    
    $directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $languages = Doctrine_Core::getTable('Language')->getAll(); 
    
    $roles = Doctrine_Core::getTable('UserRol')->getAll();
    
    $selected_directories = Doctrine_Core::getTable('MasterDirectory')->getByUser($request->getParameter('id'));
    
    $selected_directories_ids = array();
    
    foreach ($selected_directories as $selected_directory){
    	array_push($selected_directories_ids, $selected_directory->getId());
    }
    
    return $this->renderPartial('users_container', array('form' => $form, 'directories' => $directories, 'users' => $users, 'roles' => $roles, 'selected_directories_ids' => $selected_directories_ids));
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new UserForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    
  	$form = new UserForm();
  	
  	$this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new UserForm();
    
    $this->processForm($request, $this->form);
    
    $action_str = "User created ID: " . $form->getObject()->getId();
    DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));     	    
    
    $directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $roles = Doctrine_Core::getTable('UserRol')->getAll();
    
    $users = Doctrine_Core::getTable('User')->getAll();

    $this->getUser()->setFlash('test_flash', 'User saved', false);
    
    $selected_directories_ids = array();
    
    //$this->setTemplate('new');
  	return $this->renderPartial('users_container', array('form' => $form, 'directories' => $directories, 'users' => $users, 'roles' => $roles, 'selected_directories_ids' => $selected_directories_ids));
    
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
    $this->form = new UserForm($user);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
    $this->form = new UserForm($user);
    
    $form = new UserForm();

    $this->processForm($request, $this->form);

    $action_str = "User updated ID: " . $this->form->getObject()->getId();
    DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));     	    
    
    $directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $roles = Doctrine_Core::getTable('UserRol')->getAll();
    
    $users = Doctrine_Core::getTable('User')->getAll();

    $this->getUser()->setFlash('test_flash', 'User saved', false);
    
    $selected_directories_ids = array();
    
    //$this->setTemplate('new');
  	return $this->renderPartial('users_container', array('form' => $form, 'directories' => $directories, 'users' => $users, 'roles' => $roles, 'selected_directories_ids' => $selected_directories_ids));    
    
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
    $this->form = new UserForm($user);
    
    $form = new UserForm();

    //unlink associated directory
  	$asigned_directories = Doctrine_Core::getTable('MasterDirectory')->getByUser($request->getParameter('id'));
      
    foreach ($asigned_directories as $d){
    	$d->setUserId(null);
      	$d->save();
    }
    
    $user->delete();

    $action_str = "User deleted ID: " . $request->getParameter('id');
    DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));     	    
    
    $directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $roles = Doctrine_Core::getTable('UserRol')->getAll();
    
    $users = Doctrine_Core::getTable('User')->getAll();

    $this->getUser()->setFlash('test_flash', 'User deleted', false);
    
    $selected_directories_ids = array();
    
    //$this->setTemplate('new');
  	return $this->renderPartial('users_container', array('form' => $form, 'directories' => $directories, 'users' => $users, 'roles' => $roles, 'selected_directories_ids' => $selected_directories_ids));    
    
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
    	//check if the login name already exists
     // $existing_login = Doctrine_Core::getTable('User')->getUserByLogin();
    	
      $password = $request->getParameter("the_user_password");
    	
      $user = $form->save();
      
     
      $user_obj = Doctrine_Core::getTable('User')->find($user->getId());
      
      if ($password != ""){
      
      	$user_obj->setPassword(md5($password));
      
      	$user_obj->save();
      }
      
      //unset directories asigned to the user
      $asigned_directories = Doctrine_Core::getTable('MasterDirectory')->getByUser($user_obj->getId());
      
      foreach ($asigned_directories as $d){
      	$d->setUserId(null);
      	$d->save();
      }
      
      
      $directories = $request->getParameter("directories");
      
      if (count($directories) > 0){
      
	      foreach ($directories as $directory_id){
	      	$directory = Doctrine_Core::getTable('MasterDirectory')->find($directory_id);
	      	$directory->setUserId($user->getId());
	      	$directory->save();
	      }
      }

    }
    else{
    	echo 'not valid'; exit();
    }
  }
}
